package cn.net.xyan.easy.gwt.shared;

/**
 * Created by zarra on 16/2/16.
 */
public class StringUtils {
    public static String format(String text, Object... params) {
        for (int i = 0; i < params.length; i++) {
            Object p = params[i];
            if (p == null) {
                p = "";
            }
            text = text.replaceAll("\\{" + i + "}", safeRegexReplacement(p.toString()));
        }
        return text;
    }


    public static String px(int px){
        return format("{0}px",px);
    }


    /**
     * Replace any \ or $ characters in the replacement string with an escaped \\
     * or \$.
     *
     * @param replacement the regular expression replacement text
     * @return null if replacement is null or the result of escaping all \ and $
     *         characters
     */
    private static String safeRegexReplacement(String replacement) {
        if (replacement == null) {
            return replacement;
        }

        return replacement.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\$", "\\\\\\$");
    }

    public static boolean isEmpty(String string){
        return string == null || string.length() ==0;
    }
}
